package com.zimmur.app.mocolink.background.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.app.mocolink.background.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.background.common.StatusCodeEnum;
import com.zimmur.app.mocolink.background.util.WanYiNoteUtil;
import com.zimmur.app.mocolink.domain.common.Constant;
import com.zimmur.app.mocolink.domain.form.OrderForm;
import com.zimmur.app.mocolink.domain.model.AppUser;
import com.zimmur.app.mocolink.domain.model.Order;
import com.zimmur.app.mocolink.domain.model.UserAddress;
import com.zimmur.app.mocolink.domain.param.OrderParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.OrderVo;
import com.zimmur.app.mocolink.domain.vo.RefundOrderVo;
import com.zimmur.app.mocolink.service.IAppUserService;
import com.zimmur.app.mocolink.service.IOrderService;
import com.zimmur.app.mocolink.service.IRefundOrderService;
import com.zimmur.app.mocolink.service.IShoppingCartService;
import com.zimmur.app.mocolink.service.IUserAddressService;

@RestController
@RequestMapping("/restful/order")
public class OrderController extends BaseController{
	private static final Logger log = LoggerFactory.getLogger(OrderController.class);
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IShoppingCartService shoppingCartService;
	@Autowired
	private IUserAddressService userAddressService;
	@Autowired
	private IRefundOrderService refundOrderService;
	@Autowired
	private IAppUserService appUserService; 
	@RequestMapping("/addOrder")
	public ReturnJsonEntity addOrder(OrderForm orderForm){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer addressId = orderForm.getAddressId()==null?0:orderForm.getAddressId();
		String cartIds = orderForm.getCartIds();
		UserAddress userAddress=userAddressService.selectById(addressId);
		if(userAddress==null){
			codeEnum = StatusCodeEnum.CODE410401;
		}else if(StringUtils.isBlank(cartIds)){
			codeEnum = StatusCodeEnum.CODE410402;
		}else{
			String[] cartIdArray = cartIds.split(",");
			int flag = orderService.addOrder(userAddress, cartIdArray, userId,orderForm.getRemark());
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
				//发送通知短信
				sendNote(userId);
			}else if(flag == -2){
				codeEnum = StatusCodeEnum.CODE410403;
			}else if(flag == -1){
				codeEnum = StatusCodeEnum.CODE410404;
			}else if(flag == -3){
				codeEnum = StatusCodeEnum.CODE410402;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/refundList")
	public ReturnJsonEntity refundList(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer pageNo = getParam("pageNo", 1);
		PageUtil<RefundOrderVo> page = refundOrderService.selectByUserId(userId, pageNo);
		entity.setData(page);
		codeEnum = StatusCodeEnum.CODE000000;
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addRefund")
	public ReturnJsonEntity addRefund(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		String orderInfos = getParam("orderInfos");
		String summary = getParam("summary");
		Integer orderId = getParam("orderId",0);
		Order  order = orderService.selectByOrderId(orderId);
		if(order==null || order.getStatus().equals(Constant.Order_Status_Wait_Pay)){
			codeEnum = StatusCodeEnum.CODE410411;
		}else{
			int flag = refundOrderService.addRefundOrder(userId, orderInfos, summary,order);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum = StatusCodeEnum.CODE410412;
			}else if(flag == -2){
				codeEnum = StatusCodeEnum.CODE410412;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/signOrder")
	public ReturnJsonEntity signOrder(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer orderId= getIntParam("orderId");
		Order order = orderService.selectByOrderId(orderId);
		if(order!=null){
			if(Constant.Order_Status_Wait_Sign.equals(order.getStatus())){
				order.setStatus(Constant.Order_Status_Wait_Comment);
				order.setStatusName(Constant.Order_Status_Wait_Comment_Text);
				int flag = orderService.updateOrder(order);
				if(flag>0){
					codeEnum = StatusCodeEnum.CODE000000;
				}
			}else{
				codeEnum = StatusCodeEnum.CODE410406;
			}
		}else{
			codeEnum = StatusCodeEnum.CODE410405;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryOrder")
	public ReturnJsonEntity queryOrder(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer type=getIntParam("type");
		Integer pageNo = getParam("pageNo", 1);
		String status=null;
		switch (type) {
		case 1:
			status=Constant.Order_Status_Wait_Pay;
			break;
		case 2:
			status=Constant.Order_Status_Wait_Send;
			break;
		case 3:
			status=Constant.Order_Status_Wait_Sign;
			break;
		case 4:
			status=Constant.Order_Status_Wait_Comment;
			break;
		case 5:
			status=Constant.Order_Status_Wait_Comment+","+Constant.Order_Status_Done;
			break;
		default:
			break;
		}
		OrderParam param = new OrderParam();
		param.setPageNo(pageNo);
		param.setStatus(status);
		param.setUserId(userId);
		PageUtil<OrderVo> page = orderService.selectByParam(param);
		entity.setData(page);
		codeEnum = StatusCodeEnum.CODE000000;
		entity.init(codeEnum);
		return entity;
	}
	/**
	 * 发送下单通知短信
	 *@Description
	 *@param userId 
	 *@author zcx
	 *@Time 2017年10月23日 下午3:00:35
	 */
	private void sendNote(Integer userId){
		try {
			AppUser appUser = appUserService.selectById(userId);
			if(appUser!=null && !StringUtils.isBlank(appUser.getMobileNo())){
				WanYiNoteUtil.sendOperationNote(appUser.getMobileNo());
			}
		} catch (Exception e) {
			log.error("create order send note error，info:{}",e);
		}
	}
}
